package com.example.personal.aiagent.user.model.dto;

import com.baomidou.mybatisplus.annotation.TableName;
import com.example.personal.aiagent.user.model.entity.UserEntity;
import com.example.personal.aiagent.user.model.vo.UserRequest;
import com.example.personal.aiagent.user.model.vo.UserResponse;
import com.example.personal.aiagent.xunfei.account.model.dto.AccountDTO;
import com.example.personal.aiagent.xunfei.account.model.entity.AccountEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 用户传输模型
 */
@Data
@ToString(exclude="password")
@AllArgsConstructor
@NoArgsConstructor
public class UserDTO implements UserDetails {
    private Integer id;

    private String username;

    private String password;

    private String createTimeDisplay;

    private String modifyTimeDisplay;

    private Integer createTimestamp;

    private Integer modifyTimestamp;

    private String roles;

    private AccountDTO accountDTO;

    public static UserDTO entityToDTO(UserEntity entity) {
        if (entity == null) {
            return null;
        }
        UserDTO dto = new UserDTO();
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }

    public static UserDTO entityToDTO(UserEntity entity, AccountEntity account) {
        if (entity == null) {
            return null;
        }
        UserDTO dto = new UserDTO();
        BeanUtils.copyProperties(entity, dto);
        if (account != null) {
            dto.accountDTO = AccountDTO.entityToDTO(account);
        }
        return dto;
    }

    public static UserDTO voToDTO(UserRequest request) {
        UserDTO dto = new UserDTO();
        dto.setUsername(request.getUsername());
        dto.setPassword(request.getPassword());
        return dto;
    }

    public static UserDTO voToDTO(UserRequest request, int id) {
        UserDTO dto = new UserDTO();
        dto.setUsername(request.getUsername());
        dto.setPassword(request.getPassword());
        dto.setId(id);
        return dto;
    }

    public static UserResponse dTOToVO(UserDTO dto) {
        UserResponse response = new UserResponse();
        BeanUtils.copyProperties(dto, response);
        response.setAccountResponse(AccountDTO.dTOToVo(dto.getAccountDTO()));
        return response;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        for (String role : StringUtils.split(roles, ',')) {
            authorities.add(new SimpleGrantedAuthority(role.trim()));
        }
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
